package com.push.mq;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.loyal.base.impl.CommandViewClickListener;
import com.loyal.kit.OutUtil;
import com.loyal.rx.impl.MultiplePermissionsListener;
import com.loyal.rx.impl.RxSubscriberListener;
import com.push.mq.base.BaseFullScreenActivity;
import com.push.mq.records.RecordService;
import com.push.mq.utils.FileUtil;

import java.io.File;

public class SplashActivity extends BaseFullScreenActivity implements MultiplePermissionsListener, RxSubscriberListener<String>, View.OnClickListener {
    private StringBuilder builder = new StringBuilder();

    @Override
    protected int actLayoutRes() {
        return R.layout.activity_splash;
    }

    @Override
    public void afterOnCreate() {
        multiplePermissions(this, PerMission.READ_PHONE_STATE,
                PerMission.CAMERA, PerMission.RECORD_AUDIO,
                PerMission.WRITE_EXTERNAL_STORAGE, PerMission.READ_EXTERNAL_STORAGE);
    }

    @Override
    public void onMultiplePermissions(@NonNull String permissionName, boolean successful, boolean shouldShow) {
        permissionName = replaceNull(permissionName);
        if (!successful) {
            switch (permissionName) {
                case PerMission.READ_PHONE_STATE:
                    builder.append("手机状态权限、");
                    break;
                case PerMission.CAMERA:
                    builder.append("相机权限、");
                    break;
                case PerMission.RECORD_AUDIO:
                    builder.append("麦克风权限");
                    break;
                case PerMission.READ_EXTERNAL_STORAGE:
                    builder.append("存储权限、");
                    break;
            }
        }
        if (!TextUtils.isEmpty(builder)) {
            showPermissionNextDialog("请开启" + builder.toString(), new CommandViewClickListener() {
                @Override
                public void onViewClick(DialogInterface dialog, View view, Object tag) {
                    if (null != dialog)
                        dialog.dismiss();
                    startActivity(new Intent(Settings.ACTION_SETTINGS)); //直接进入手机中设置界面
                    finish();
                }
            }, false);
            return;
        }
        switch (permissionName) {
            case PerMission.READ_PHONE_STATE:
                break;
            case PerMission.CAMERA:
                break;
            case PerMission.RECORD_AUDIO:
                break;
            case PerMission.READ_EXTERNAL_STORAGE:
                File apkFile = new File(FileUtil.apkPath, FileUtil.apkName);
                FileUtil.deleteFile(apkFile);
                FileUtil.deleteEmptyDirs(FileUtil.videoPath);
                FileUtil.createFileSys();
                if (Build.VERSION.SDK_INT >= 23) {
                    if (!Settings.canDrawOverlays(this)) {
                        //Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                                Uri.parse("package:" + getPackageName()));
                        startActivityForResult(intent, IntImpl.finish2Login);
                        return;
                    } else {
                        //Android6.0以上
                        checkClient();
                    }
                } else {
                    checkClient();
                }
                break;
        }
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onResult(int what, Object tag, String result) {
    }

    @Override
    public void onError(int what, Object tag, Throwable e) {
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == IntImpl.finish2Login) {
            if (Build.VERSION.SDK_INT >= 23) {
                if (!Settings.canDrawOverlays(this)) {
                    showDialog("请授予权限", true);
                } else {
                    //Android6.0以上
                    checkClient();
                }
            } else {
                checkClient();
            }
        }
    }

    private void checkClient() {
        Intent intent=new Intent(this,RecordService.class);
        startService(intent);
        //startServiceByAct(RecordService.class);
        finish();
    }
}
